from django.shortcuts import render

# Create your views here.
from rest_framework.viewsets import ViewSetMixin,GenericViewSet
from rest_framework.generics import ListAPIView
from rest_framework.mixins import ListModelMixin,RetrieveModelMixin
from .models import CourseCategory,Course,CourseChapter
from .serializer import CourseCategoryModelSerializer,CourseModelSerializer
from rest_framework.filters import OrderingFilter,SearchFilter
from django_filters.rest_framework import DjangoFilterBackend
from .pagination import CommonPageNumberPagination
from .filter import MyFilter,CourseFilterSet


# 课程分类视图
class CourseCategoryView(ViewSetMixin,ListAPIView):
    queryset = CourseCategory.objects.all()
    serializer_class = CourseCategoryModelSerializer


# 课程群查视图
class CourseView(ViewSetMixin, ListModelMixin,RetrieveModelMixin):
    queryset = Course.objects.all()
    serializer_class = CourseCategoryModelSerializer
    # 排序功能,过滤功能
    filter_backends = [OrderingFilter,DjangoFilterBackend]
    ordering_fields = ['id','price','students']
    # DjangoFilterBackend需要配套filter_fields使用
    # filter_fields = ('course_category',)
    # 也可以配套一个类使用，在类中写要过滤的字段
    filter_fields = CourseFilterSet
    # 分页
    pagination_class = CommonPageNumberPagination

# 章节视图
class CourseChapterView(GenericViewSet,ListModelMixin):
    queryset = CourseChapter.objects.all()
    serializer_class = CourseChapterModelSerializer
    # 配置过滤类，以课程作为过滤条件
    filter_backends = [DjangoFilterBackend,]
    lookup_field = ('course',)


# 搜索视图
class CourseSearchView(GenericViewSet,ListModelMixin):
    queryset = Course.objects.all()
    serializer_class = CourseModelSerializer

    filter_backends = [SearchFilter,]
    pagination_class = CommonPageNumberPagination
    search_fields = ['name']



